/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fullalbum;

import fullalbum.bean.Figure;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author leoomoreira
 */
public final class Utils {
    
    public static List<String> getSortFigureId(List<Figure> figureList) {
        List<String> result = new ArrayList<String>();
        while (figureList.size() > 0) {
            int minLength = Integer.MAX_VALUE;
            for (Figure f : figureList) {
                if (f.getId().length() < minLength) {
                    minLength = f.getId().length();
                }
            }
            List<String> temp = new ArrayList<String>();
            for (int i = 0; i < figureList.size(); i++) {
                if (figureList.get(i).getId().length() == minLength) {
                    Figure f = figureList.remove(i);
                    temp.add(f.getId());
                    i--;
                }
            }
            String[] tempStr = new String[temp.size()];
            for (int i = 0; i < tempStr.length; i++) {
                tempStr[i] = temp.get(i);
            }
            Arrays.sort(tempStr);
            for (int i = 0; i < tempStr.length; i++) {
                result.add(tempStr[i]);
            }
        }
        return result;
    }

    public static String encrypt(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return new String(hexCodes(md.digest(password.getBytes())));
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * Method used to transform a string of bytes in the format in an array of hexadecimal characters
     *
     * @param text
     * @return
     */
    private static char[] hexCodes(byte[] text) {
        char[] hexOutput = new char[text.length * 2];
        String hexString;
        for (int i = 0; i < text.length; i++) {
            hexString = "00" + Integer.toHexString(text[i]);
            hexString.getChars(hexString.length() - 2, hexString.length(), hexOutput, i * 2);
        }
        return hexOutput;
    }
}
